cd "path_to_replication_folder"
sysdir set PLUS "path_to_nondefault_stata_packages"


* balance sheet data cleaning: 
** drop firms that always make negative profits 
** drop highest and lowest 1% of profit ratios

u balance_sheets, clear
drop if p9000<0 | p9000==.
g dum=1 if p9050<=0 & p9050!=.
egen nm = count(dum) , by(lnr) 
bysort lnr: g N=_N
drop if nm==N // drop firms that always make negative profits
drop nm N
egen R=total(p9000), by(bof year)
g mr=p9000/R
g sr2=(p9050)/p9000
forvalues i=2005(1)2018 {
	sum sr2 if year==`i', detail
	global p1_2=r(p1)
	global p99_2=r(p99)
	replace sr2=. if (sr2>$p99_2 | sr2<$p1_2) & year==`i'
}

rename lnr_as lnr_j
save balance_sheetsw, replace







*** make sample of large owners with consistent id over time

u u, clear
keep if s>.05
save u_small, replace

* make a list of individuals that a some point own more than 5 % in some firm
forvalues i=2005(1)2018 {
u u_small if year==`i' , clear
keep if s>.05
keep if lnr!=. | lnr_for!=.
drop lnr_orgnr
keep lnr lnr_for
duplicates drop
if `i'>2005 {
	append using powners
}
save powners, replace
}
*make an indentifier for individuals that is consistent over time and covers domestic and foreign owners
duplicates drop
duplicates tag lnr, g(tag)
tab tag
sort lnr lnr_for
g id=_n
drop tag*
save powners, replace
keep if lnr!=.
save dpowners, replace

forvalues i=2005(1)2018 {
u u_small if year==`i' , clear
keep if lnr!=. 
keep lnr s year lnr_as_orgnr
merge m:1 lnr using dpowners
keep if _me==3
drop _mer
compress
save dpowners_shares`i', replace
}

u dpowners_shares2005, clear
forvalues i=2006(1)2018 {
append using dpowners_shares`i'
erase dpowners_shares`i'.dta
}
compress
sort lnr year
save dpowners_shares, replace


erase dpowners_shares2005.dta
erase powners.dta
erase dpowners.dta


*** prepare common ownership measure TotalCrossOwn


forvalues i=2005(1)2018 {								
u u_small if year==`i' , clear
keep if lnr!=. | lnr_for!=.
g s05=s if s>.05
g s1=s if s>.1
drop s
keep lnr lnr_for year lnr_as_orgnr s0* s1
merge m:1 lnr year using lnr_hh_y, keep(match master)
drop _mer
rename lnr_as_orgnr lnr_orgnr
replace lnr=. if hh!=.
egen hhi=group(lnr lnr_for hh), missing
collapse (sum) s*, by(hhi lnr_o)
sort hhi
save temp, replace
u temp, clear
rename lnr_o lnr_i
rename s* s*_i
sort hhi
joinby hhi using temp
rename lnr_o lnr_j
rename s* s*_j
foreach x in 05 1  {
	g s`x'=s`x'_j*s`x'_i
	replace s`x'=. if s`x'==0
}
drop if lnr_i==lnr_j

collapse (sum) s05 s1 (count) hh05=s05 hh1=s1, by(lnr_i lnr_j)
g year=`i'
compress
save k_ij_`i', replace
}



forvalues i=2005(1)2018 {
u k_ij_`i', clear
rename (lnr_i lnr_j) (lnr_j lnr_jj)
merge m:1 lnr_j year using balance_sheetsw, keep(match) 
drop _mer
destring bof, replace
rename bof sec_i
rename (lnr_j lnr_jj) (lnr_i lnr_j)
merge m:1 lnr_j year using balance_sheetsw,  keep(match)
drop _mer
destring bof, replace
rename bof sec_j
keep if sec_i==sec_j
collapse (sum) s0* s1 hh*, by(lnr_i sec_i sec_j year)
if `i'>2005 {
	append using kraw_ii
}
save kraw_ii, replace
}



** prep marriage indicator for 1st stage of 2SLS



u marriages, clear
drop if year<2004 
duplicates drop lnr year, force
merge 1:m lnr year using dpowners_shares 
keep if _merge==3
drop _merge

drop id lnr_for
rename lnr lnr_pp
rename s s_i
rename lnr_a lnr_i
rename lnr_p lnr
sort lnr year
joinby lnr year using dpowners_shares
rename s s_j
rename lnr_a lnr_j

g s05_i=s_i if s_i>.05
g s1_i=s_i if s_i>.1
g s05_j=s_j if s_j>.05
g s1_j=s_j if s_j>.1
drop s_i s_j

egen mx=rowmax(lnr lnr_p)
egen mn=rowmin(lnr lnr_p)
duplicates drop mn mx lnr_i lnr_j year, force
drop if lnr_i==lnr_j

foreach x in 05 1  {
	g s`x'=s`x'_j*s`x'_i
}

collapse (sum) s05 s1, by(lnr_i lnr_j year)

rename s* iv*
save iv_ij, replace // this is fully symmetric

duplicates drop year, force
keep year
save year, replace

u iv_ij, clear
duplicates drop lnr_i lnr_j, force
keep lnr*
cross using year
erase year.dta

forvalues i=2005(1)2018 {
merge 1:1 year lnr_i lnr_j using k_ij_`i', keepusing(s*) update keep(match master match_up) nogen
}
merge 1:1 lnr_i lnr_j year using iv_ij
drop _m
rename iv* marry*

compress
sort lnr_i lnr_j year

foreach var of varlist s* {
replace `var'=0 if `var'==.
}


foreach x in 05 1 {
g M`x'=marry`x'
sort lnr_i lnr_j year
bysort lnr_i lnr_j: replace M`x'=M`x'[_n-1] if M`x'[_n-1]!=. 
replace M`x'=0 if M`x'==.
}

rename (lnr_i lnr_j) (lnr_j lnr_i)

merge m:1 lnr_j year using balance_sheetsw, keepusing(bof)
drop if _merge==2
drop _merge
rename bof sec_i

rename (lnr_i lnr_j) (lnr_j lnr_i)
merge m:1 lnr_j year using balance_sheetsw, keepusing(bof)
drop if _merge==2
drop _merge
rename bof sec_j
compress

drop if lnr_i==lnr_j
keep if sec_i==sec_j

foreach var of varlist M* {
	g U`var'=1 if `var'>0
	}

collapse (sum) U*, by(lnr_i year)

save alt_iv, replace


** prep k-indices for calculation of Delta MHHI


u k_ij_2016, clear
rename (lnr_i lnr_j) (lnr_j lnr_jj)
merge m:1 lnr_j year using balance_sheetsw, keep(match) 
drop _mer
destring bof, replace
rename bof sec_i
rename mr mr_i
rename (lnr_j lnr_jj) (lnr_i lnr_j)
merge m:1 lnr_j year using balance_sheetsw,  keep(match)
drop _mer
destring bof, replace
rename bof sec_j
rename mr mr_j
rename lnr_i lnr_as_orgnr
merge m:1 lnr_as using ownership_structure, keepusing(sq)
drop if _mer==2
drop _m

foreach x in 05 1  {
	replace s`x'=s`x'*mr_j*mr_i/sq
}
collapse (sum) s0* s1, by(sec_i sec_j year)
keep if sec_i==sec_j
save ksr_ii2016, replace  // includes private individuals and foreigners








*** common ownership measure including all owners

u u if year==2016, clear	
keep if s>.01
g s05=s if s>.05
g s1=s if s>.1
keep lnr lnr_for  year lnr_as_orgnr s0* s1 lnr_o
merge m:1 lnr year using lnr_hh_y, keep(match master)
drop _mer
replace lnr=. if hh!=.
egen hhi=group(lnr lnr_f lnr_o hh), missing
drop lnr_org
rename lnr_as_orgnr lnr_orgnr
collapse (sum) s*, by(hhi lnr_o)

sort hhi
save temp, replace
u temp, clear
rename lnr_o lnr_i
rename s* s*_i
sort hhi
joinby hhi using temp
rename lnr_o lnr_j
rename s* s*_j
foreach x in 05 1  {
	g s`x'=s`x'_j*s`x'_i
	replace s`x'=. if s`x'==0
}
drop if lnr_i==lnr_j

collapse (sum)  s05 s1, by(lnr_i lnr_j)
g year=2016
compress
save m_ij_2016, replace


u m_ij_2016, clear
rename (lnr_i lnr_j) (lnr_j lnr_jj)
merge m:1 lnr_j year using balance_sheetsw, keep(match) 
drop _mer
destring bof, replace
rename bof sec_i
rename mr mr_i
rename (lnr_j lnr_jj) (lnr_i lnr_j)
merge m:1 lnr_j year using balance_sheetsw,  keep(match)
drop _mer
destring bof, replace
rename bof sec_j
rename mr mr_j
rename lnr_i lnr_as_orgnr
merge m:1 lnr_as using ownership_structure, keepusing(sq)
drop if _mer==2
drop _m

foreach x in 05 1  {
	replace s`x'=s`x'*mr_j*mr_i/sq
}
collapse (sum) s0* s1, by(sec_i sec_j year)
keep if sec_i==sec_j
save msr_ii2016, replace  


*** common ownership measure using direct shares

u direct_shares, clear
rename nom s
drop hh
keep if s>.01
g s05=s if s>.05
g s1=s if s>.1
sort lnr year
joinby lnr year using lnr_hh_y, unmatched(master)
drop _mer

replace lnr=. if hh!=.
egen hhi=group(hh lnr lnr_o lnr_for), missing
drop lnr_o
rename lnr_as_orgnr lnr_orgnr
collapse (sum) s*, by(hhi lnr_o)
sort hhi
save temp, replace
rename lnr_o lnr_i
rename s* s*_i
sort hhi
joinby hhi using temp
rename lnr_o lnr_j
rename s* s*_j
foreach x in 05 1  {
	g s`x'=s`x'_j*s`x'_i
	replace s`x'=. if s`x'==0
}
drop if lnr_i==lnr_j
collapse (sum) s05 s1 , by(lnr_i lnr_j)
g year=2016
compress
save j_ij_2016, replace





u j_ij_2016, clear
rename (lnr_i lnr_j) (lnr_j lnr_jj)
merge m:1 lnr_j year using balance_sheetsw, keep(match) 
drop _mer
destring bof, replace
rename bof sec_i
rename mr mr_i
rename (lnr_j lnr_jj) (lnr_i lnr_j)
merge m:1 lnr_j year using balance_sheetsw,  keep(match)
drop _mer
destring bof, replace
rename bof sec_j
rename mr mr_j
rename lnr_i lnr_as_orgnr
merge m:1 lnr_as using ownership_structure, keepusing(sqd)
drop if _mer==2
drop _m

foreach x in 05 1  {
	replace s`x'=s`x'*mr_j*mr_i/sq
}
collapse (sum) s0* s1, by(sec_i sec_j year)
keep if sec_i==sec_j
save jsr_ii2016, replace  

** cleaning up 

erase m_ij_2016.dta
erase j_ij_2016.dta
erase temp.dta
erase iv_ij.dta

forvalues i=2005(1)2018 {
erase k_ij_`i'.dta
}

erase u_small.dta

